<?php

namespace App\Http\Controllers\v1;

use App\Models\User;
use App\Models\Role;
use Illuminate\Http\Request;
use Illuminate\Http\Exception\HttpResponseException;
use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;
use Illuminate\Http\Response as IlluminateResponse;
use JWTFactory;
use App\Http\Controllers\Controller;
use App\DB\UserDB;
use App\DB\PermissionDB;

class UserController extends Controller {
	/**
	 * Create a new controller instance.
	 *
	 * @return void
	 */
	protected $app;
	
	/**
	 *
	 * @param \Laravel\Lumen\Application $app        	
	 */
	public function __construct(\Laravel\Lumen\Application $app) {
		$this->app = $app;
	}
	
	/**
	 * 查询用户信息
	 *
	 * @param Request $request        	
	 * @param string $uid        	
	 */
	public function query(Request $request, $uid = null) {
		if (! array_contain_value ( PermissionDB::getUserRoles ( $request->user ()->uid ), '02' )) {
			return responseData ( [ ] );
		}
		
		$users = UserDB::getUserByUid ( $uid );
		return responseData ( $users );
	}
	
	/**
	 * 查询当前登录用户信息
	 *
	 * @param Request $request        	
	 * @param string $uid        	
	 */
	public function current(Request $request) {
		$user = $request->user ();
		return responseData ( $user );
	}
	
	/**
	 * 添加用户信息
	 *
	 * @param Request $request        	
	 */
	public function add(Request $request) {
		$jsondata = $request->getContent ();
		$data = json_decode ( $jsondata );
		
		UserDB::addUser ( $data );
		return responseError ();
	}
	
	/**
	 * 删除用户信息
	 *
	 * @param Request $request        	
	 */
	public function delete(Request $request) {
		$jsondata = $request->getContent ();
		$data = json_decode ( $jsondata );
		
		UserDB::deleteUser ( $data );
		return responseError ();
	}
	
	/**
	 * 更新用户信息
	 *
	 * @param Request $request        	
	 */
	public function update(Request $request) {
		$jsondata = $request->getContent ();
		$data = json_decode ( $jsondata );
		
		UserDB::updateUser ( $data );
		return responseError ();
	}
}
